package com.example.brazil_project.util;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;














import com.example.brazil_project.parameter.Parameter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.os.Environment;


public class BitmapUtil {
	
	/**根据SD卡图片获取Bitmap
	 * Sd卡图片路径/mnt/sdcard之后的路径就行
	 * @author charlie1
	 *
	 */
	 public static Bitmap getBitmapFromSD(String filePath){
		   String SDCarePath=Environment.getExternalStorageDirectory().toString(); 
		   filePath = SDCarePath+filePath;
		   Bitmap Bitmap = BitmapFactory.decodeFile(filePath, null); 
		   
		return Bitmap;
		     
	   }
	   
	 	/**根据assets里面图片获取Bitmap
		 *
		 * @author charlie1
		 *
		 */
		 public static Bitmap getBitmapFromAssets(String filePath){
			 //"/assets/assets/img/"
			 Activity Contx =  Parameter.openingContx;
			 try {
			//String[] files = Parameter.openingContx.getAssets().list("assets/img");
			 BufferedInputStream bfs = new BufferedInputStream(Contx.getAssets().open(filePath));//assets/img/*.jpg
			 Bitmap bmp=BitmapFactory.decodeStream(bfs);  
		      bfs.close();
			return bmp;
			 } catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			return null;   
			     
		   }
	   /***
	    * 图片的缩放方法
	    * 
	    * @param bgimage
	    *            ：源图片资源
	    * @param newWidth
	    *            ：缩放后宽度
	    * @param newHeight
	    *            ：缩放后高度
	    * @return
	    */
	   public static Bitmap zoomImage(Bitmap bgimage, double newWidth,
	                   double newHeight) {
	           // 获取这个图片的宽和高
	           float width = bgimage.getWidth();
	           float height = bgimage.getHeight();
	           // 创建操作图片用的matrix对象
	           Matrix matrix = new Matrix();
	           // 计算宽高缩放率
	           float scaleWidth = ((float) newWidth) / width;
	           float scaleHeight = ((float) newHeight) / height;
	           // 缩放图片动作
	           matrix.postScale(scaleWidth, scaleHeight);
	           Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,
	                           (int) height, matrix, true);
	           return bitmap;
	   }
	   
	   //网络图片
	   public static Bitmap returnBitMap(String url) {
		   Bitmap bitmap = null;
			InputStream in = null;
			BufferedOutputStream out = null;
			try
			{
				in = new BufferedInputStream(new URL(url).openStream(), 1024);
				final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
				out = new BufferedOutputStream(dataStream, 1024);
			
				copy(in, out);
				out.flush();
				byte[] data = dataStream.toByteArray();
				bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
				data = null;
				return bitmap;
			}
			catch (IOException e)
			{
				e.printStackTrace();
				return null;
			}
	   }
	   
	   private static void copy(InputStream in, OutputStream out)
	            throws IOException {
	        byte[] b = new byte[1024];
	        int read;
	        while ((read = in.read(b)) != -1) {
	            out.write(b, 0, read);
	        }
	    }
	  public  static Bitmap getBitmap(String url){
		  if(url.startsWith("http:")){
			  return returnBitMap(url);
		  }
		  if(url.startsWith("assets")){
			  return getBitmapFromAssets(url);
		  }
		 
		  return getBitmapFromSD(url);
		  
		  
	  }
	  /**
	   * 用drawable放回Bitmap
	   * @param drawable
	   * @return
	   */
	  public static Bitmap drawableToBitmap(Drawable drawable)
	  {
	  Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
	  Canvas canvas = new Canvas(bitmap);
	  drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
	  drawable.draw(canvas);
	  return bitmap;
	  } 
	   
}
